﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kooboo.IoC;
using Kooboo.Commerce;


namespace Kooboo.Web.Areas.Commerce.Binders
{
    public class OrderBinder:EntityBinder
    {
        protected override string[] SkipProperties
        {
            get
            {
                return new string[] { "Promotions","OrderItems", };
            }
        }

        protected override object CreateModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, Type modelType)
        {
            var model = ObjectContainer.CreateInstance<IOrder>();

            var form = controllerContext.HttpContext.Request.Form;

            model.OrderItems = new List<IOrderItem>();

            var orderItemsIndexes = form["OrderItems.index"];

            if (orderItemsIndexes != null)
            {
                foreach (var i in orderItemsIndexes.Split(','))
                {
                    var orderItem = ObjectContainer.CreateInstance<IOrderItem>();

                    var name = "OrderItems[" + i + "]";

                    orderItem.Id = -1;
                    orderItem.ProductId = int.Parse(form[name + ".ProductId"]);
                    orderItem.SaleItemId = int.Parse(form[name + ".SaleItemId"]);
                    orderItem.ProductName = form[name + ".ProductName"];
                    orderItem.Quantity = int.Parse(form[name + ".Quantity"]);
                    orderItem.UnitPrice = int.Parse(form[name + ".UnitPrice"]);

                    orderItem.OrderItemCustomFields = new List<IOrderItemCustomField>();
                    var customerFieldIndexes = form[name + ".OrderItemCustomFields.index"];
                    if (customerFieldIndexes != null)
                    {
                        foreach(var n in customerFieldIndexes.Split(','))
                        {
                            var fieldName = name + ".OrderItemCustomFields[" + n + "]";

                            var field = ObjectContainer.CreateInstance<IOrderItemCustomField>();
                            field.Id = -1;
                            field.Name = form[fieldName + ".Name"];
                            field.Value = form[fieldName + ".Value"];

                            orderItem.OrderItemCustomFields.Add(field);
                        }
                    }

                    orderItem.OrderItemVariations = new List<IOrderItemVariation>();

                    var orderItemVariationIndexes = form[name + ".OrderItemVariations.index"];
                    if (orderItemVariationIndexes != null)
                    {
                        foreach (var n in orderItemVariationIndexes.Split(','))
                        {
                            var variationName = name + ".OrderItemVariations[" + n + "]";

                            var variation = ObjectContainer.CreateInstance<IOrderItemVariation>();
                            variation.Id = -1;
                            variation.Name = form[variationName + ".Name"];
                            variation.Value = form[variationName + ".Value"];

                            orderItem.OrderItemVariations.Add(variation);
                        }
                    }

                    model.OrderItems.Add(orderItem);
                 
                }
            }
            return model;
        }
    }
}